package beanstao.util.socket.protocol;

import beanstao.util.exception.technical.util.SocketDoorException;

/**
 * 一个描述协议路由的类型
 * 
 * @author beanstao@google.com 
 * 
 */
public enum ProtocolRoutingType
{
	NORMAL(0), ROUTED(1), IGNORE_CHECKS(2);

	private final int routingTypeCode;

	private ProtocolRoutingType(int routingType)
	{
		this.routingTypeCode = routingType;
	}

	/**
	 * ignore checks takes precedence over should_route and force it to be local store.
	 * 
	 * @param should_route
	 * @param ignore_checks
	 * @return
	 */
	public static ProtocolRoutingType getRequestRoutingType(boolean should_route, boolean ignore_checks)
	{
		if (ignore_checks)
		{
			return ProtocolRoutingType.IGNORE_CHECKS;
		}
		else if (should_route)
		{
			return ProtocolRoutingType.ROUTED;
		}

		return ProtocolRoutingType.NORMAL;
	}

	public int getRoutingTypeCode()
	{
		return this.routingTypeCode;
	}

	/**
	 * ignore checks takes precedence over should_route and force it to be local store.
	 * 
	 * @param should_route
	 * @param ignore_checks
	 * @return
	 */
	public static ProtocolRoutingType getRequestRoutingType(int routingCode)
	{
		switch (routingCode)
		{
			case 0:
				return ProtocolRoutingType.NORMAL;
			case 1:
				return ProtocolRoutingType.ROUTED;
			case 2:
				return ProtocolRoutingType.IGNORE_CHECKS;
		}

		throw new SocketDoorException("Invalid ProtocolRoutingType code passed " + routingCode);
	}
}
